/**
 * PK10的逻辑处理
 */
(function() {
    var currTabId = 1;
    var tabDraws = [
        // 1：两面
        fillLM,
        
        // 2 组合
        fillZuHe,

        // 3 1~5
        function() {
            fill_table(1, 5);
        },

        // 4 6~10
        function() {
            fill_table(6, 10);
        },
    ];

    function fillLM() {
        var types = ["大", "小", "单", "双"];
        for (var i = 0; i < types.length; i++) {
            var t = types[i];
            add_bet_item('冠亚' + t, '冠亚' + t, $(".page1 .gyh"));
        }

        var ths = ["冠军", "亚军", "第三名", "第四名", "第五名", "第六名", "第七名", "第八名", "第九名", "第十名"];
        var arr = ['冠', '亚', '三', '四', '五', '六', '七', '八', '九', '十'];
        var types = ["大", "小", "单", "双", "龙", "虎"];
        
        var div = $(".ten");
        div.empty();
        for (var j = 0; j < ths.length; j++) {
            add_bet_th(ths[j], div);
    
            if (j < 5)
                var ul = $('<ul class="am-avg-sm-3 bet"></ul>');
            else
                var ul = $('<ul class="am-avg-sm-2 bet"></ul>');
    
            add_bet_item(arr[j] + '单', '单', ul);
            add_bet_item(arr[j] + '大', '大', ul);
            if ( j < 5) {
                add_bet_item(arr[j] + '龙', '龙', ul);
            }
            add_bet_item(arr[j] + '双', '双', ul);
            add_bet_item(arr[j] + '小', '小', ul);
            if ( j < 5) {
                add_bet_item(arr[j] + '虎', '虎', ul);
            }
    
            div.append(ul);
        }
    }

    function fillZuHe() {
        var types = ["大", "小", "单", "双"];
        for (var i = 0; i < types.length; i++) {
            var t = types[i];
            add_bet_item('冠亚' + t, '冠亚' + t, $(".page2 .gyh"));
        }

        var ul = $('<ul class="am-avg-sm-3 bet"></ul>');
        $(".gyh2").append(ul);
        for (var i = 3; i <= 19; i++) {
            add_bet_item("冠亚" + i, i, ul);
            if (i == 17) {
                ul = $('<ul class="am-avg-sm-2 bet"></ul>');
                $(".gyh2").append(ul);
            }
        }
    }

    // 填充投注区域表格
    function fill_table(start, end) {
        var arr = ['冠', '亚', '三', '四', '五', '六', '七', '八', '九', '十'];
        var ths = ["冠军", "亚军", "第三名", "第四名", "第五名", "第六名", "第七名", "第八名", "第九名", "第十名"];
        var arr2 = ['一', '二', '三', '四', '五', '六', '七', '八', '九', '十'];
        var div = start == 1 ? $(".page3") : $(".page4");
        for (var j = start - 1; j < end; j++) {
            add_bet_th(ths[j], div);
            var ul = $('<ul class="am-avg-sm-2 bet"></ul>');
            for (var i = 1; i <= 10; i++) {
                var t = '<span class="bet-content round-7 data-__i__">__i__</span>'.replace(/__i__/g, i);
                add_bet_item(arr[j] + arr2[i - 1], t, ul);
            }
            div.append(ul);
        }   
        div.append('<div class="line"></div>');
    }

    var initF = function(game, first) {
        window.game = game;
        initGame(game, function() {
            if (first !== false) fill_tabs(['两面', '冠亚和', '1-5名', '6-10名']);
            currTabId = parseInt($(".menu-left a.active").attr("value"));
            $(".page" + currTabId).attr("flag", 1);
            var f = tabDraws[currTabId - 1];
            f();
        }, first);
    }

    // 绘制开奖结果
    var drawF = function(list) {
        var c = Result.last();
        if (!c || !c.red) return [];
        var red = c.red;
        var r = [];
    
        // 冠亚军和
        var gy = parseInt(red[0]) + parseInt(red[1]);
        r.push(gy);
    
        if (gy > 11) r.push('大');
        else r.push('小');
        if (gy % 2 == 0) r.push('双');
        else r.push('单');
    
        // 1~5龙虎
        for (var i = 0; i < 5; i++) {
            var v0 = parseInt(red[i]), v1 = parseInt(red[9 - i]);
            if (v0 > v1) r.push('龙');
            else r.push('虎');
        }
        return r;
    }

    // 检测能否下注，看下单号1-10是否满足要求
    var checkF = function(type, issue, data) {
        var limit = Util.getNumberLimit(type);
        if (limit <= 0 || limit >= 10) return true;
    
        var arr1 = ['冠', '亚', '三', '四', '五', '六', '七', '八', '九', '十'];
        var arr2 = ['一', '二', '三', '四', '五', '六', '七', '八', '九', '十'];
        var m = {};
        for (var k in data) {
            if (k.length != 2) continue;
            if (arr1.indexOf(k[0]) != -1 && arr2.indexOf(k[1]) != -1) {
                if (!m[k[0]]) m[k[0]] = {};
                m[k[0]][k[1]] = true;
            }
        }
        if (Util.isEmpty(m)) return true;
    
        // 获取下已经下注的单据
        var bets = context.bets[type];
        if (bets) bets = bets[issue] || [];
        else bets = [];
        for (var i in bets) {
            var k = bets[i].target;
            if (k.length != 2) continue;
            if (arr1.indexOf(k[0]) != -1 && arr2.indexOf(k[1]) != -1) {
                if (!m[k[0]]) m[k[0]] = {};
                m[k[0]][k[1]] = true;
            }
        }
    
        var total = 0;
    
        for (var k in m) {
            var m2 = m[k];
            if (Object.keys(m2).length > limit) {
                var l = "下注失败，__g__ 定位玩法，单个名次不允许下注__v__码或者__v__以上！如有疑问，请咨询客服！";
                l = l.replace(/__g__/, get_game_display_name(type));
                l = l.replace(/__v__/g, limit);
                alert2(l, true);
                return false;
            }
        }
    
        return true;
    }

    var data = {
        drawResult2: drawF,
        checkBetList: checkF,
        init: initF,
    }
    context.global.game["bjpk10"] = data;
    context.global.game["zypk10"] = data;
})();